function line_histogram_scatter(parameters,data1,data2)

label=parameters.label;

logBinFlag=parameters.logBinFlag;
logScaleFlag=parameters.logScaleFlag;
xbins=parameters.xbins;
xrange=parameters.xBinRange;
ybins=parameters.ybins;
yrange=parameters.yBinRange;
xplotRange=parameters.xplotRange;
yplotRange=parameters.yplotRange;
labelFS=parameters.labelFS;
axisFS=parameters.axisFS;
printedXsize=parameters.printedXsize;
printedYsize=parameters.printedYsize;

% function generic_scatter(logPlotFlag,numbins,numScaledBins,data1,data2)
%plot_fig=figure;
plot_fig=parameters.plot_fig;
%labelFS=10;
%axisFS=8;

set(plot_fig, 'PaperUnits', 'centimeters');
set(plot_fig, 'PaperSize', [printedXsize printedYsize]);
set(plot_fig, 'PaperPositionMode', 'manual');
set(plot_fig, 'PaperPosition', [0 0 printedXsize printedYsize]);

binReductionX=xrange(2)/xbins;
binReductionY=yrange(2)/ybins;

dens = zeros(ybins,xbins);

%This rebins the data, by increasing the count in an index of a matix.
%The bins are resized so the matix is not incredibly large
if(logBinFlag==0)
    for i=1:length(data1)
        x = max(fix(data1(i)/binReductionX),1);
        x = min(x,xbins);
        y = max(fix(data2(i)/binReductionY),1);
        y = min(y,ybins);
        dens(y,x) = dens(y,x) + 1;
    end
    fsc_axis = ([0:1:(xbins-1)]*binReductionX);
    fl1_axis = ([0:1:(ybins-1)]*binReductionY);
    yLabelStr='Fluorescence A.U.';
elseif(logBinFlag==1)
    %minLogVal determines the minimum log value that will be binned.  All
    %values below this will be compressed into the first bin.  The -1
    %corrects for a fence post issue.
    minLogVal=xrange(1)-1;
    %maxLogVal determines the maximum log value that will be binned.  All
    %values above this will be compressed into the last bin.
    maxLogVal=xrange(2);
    %shift the maximum log value
    maxShift=maxLogVal-minLogVal;    
    data1log=log10(data1);
    %Shift values so all are positive
    data1logShift=(data1log-minLogVal);
    %normalize the shifted data
    data1logShiftNorm=(data1logShift)*(xbins/maxShift);
    
    data2log=log10(data2)*(ybins/log10(yrange(2)));

    for i=1:length(data1)
        %conduct binning for x values
        x = max(fix(data1logShiftNorm(i)),1);
        %if an x value is larger than the xrange, this will compress it
        %into the last bin.
        x = min(x,xbins);

        %y = max(fix(log10(data2(i))*(ybins/log10(yrange(2)))),1);
        y = max(fix(data2log(i)),1);
        y = min(y,ybins);
        
        dens(y,x) = dens(y,x) + 1;
    end    

    [minVal deltaBin]=compute_logFscAxis(xrange,xbins);
    fsc_axis = ([minVal:deltaBin:xrange(2)]); 
    
    fl1_axis = ([0:1:(ybins-1)]*(log10(yrange(2))/ybins));
    

    yLabelStr='Fluorescence A.U.';

elseif(logBinFlag==2)
    %minLogVal determines the minimum log value that will be binned.  All
    %values below this will be compressed into the first bin.  The -1
    %corrects for a fence post issue.
    minLogVal=xrange(1)-1;
    %maxLogVal determines the maximum log value that will be binned.  All
    %values above this will be compressed into the last bin.
    maxLogVal=xrange(2);
    %shift the maximum log value
    maxShift=maxLogVal-minLogVal;    
    data1log=log10(data1);
    %Shift values so all are positive
    data1logShift=(data1log-minLogVal);
    %normalize the shifted data
    data1logShiftNorm=data1logShift*(xbins/maxShift);
    %data1logShift;
    for i=1:length(data1)
        %conduct binning for x values
        x = max(fix(data1logShiftNorm(i)),1);
        %if an x value is larger than the xrange, this will compress it
        %into the last bin.
        x = min(x,xbins);
        y = max(fix(data2(i)*(ybins/yrange(2))),1);
        y = min(y,ybins);
        dens(y,x) = dens(y,x) + 1;
    end    
    [minVal deltaBin]=compute_logFscAxis(xrange,xbins);
    fsc_axis = ([minVal:deltaBin:xrange(2)]);    
    fl1_axis = ([0:1:(ybins-1)]*(yrange(2)/ybins));
    
    yLabelStr='Fluorescence A.U.';
end
%This section normalizes the counts in each row of the matrix.  It is necessary 
%to correct for multiple inductions being placed in the
%same column

if(strcmp(parameters.interpolate,'true'))
    dens2=zeros(size(dens));
    lastNonZero=0;
    for j=1:xbins
        if(sum(dens(:,j)>0))
            dens2(:,j)=dens(:,j)/sum(dens(:,j));
            lastNonZero=j;
            %copy the data for previous entries into the missing output spaces.
            %Interpolates data
        elseif(lastNonZero>0)
            dens2(:,j)=dens2(:,lastNonZero);
        end
    end
    dens=dens2;
end

[toPlotX]=findPlottedIndices(fsc_axis,xplotRange);
[toPlotY]=findPlottedIndices(fl1_axis,yplotRange);

if(logScaleFlag==0)
    gca=imagesc(fsc_axis(toPlotX), fl1_axis(toPlotY), dens(toPlotY,toPlotX));
elseif(logScaleFlag==1)
    gca=imagesc(fsc_axis(toPlotX), fl1_axis(toPlotY), log10(dens(toPlotY,toPlotX)),parameters.cMap);
    %gca=imagesc(fsc_axis(toPlotX), fl1_axis(toPlotY), log(dens2(toPlotY,toPlotX)));
    axis xy
end

if(strcmp(parameters.colorbar,'true'))
    colorbar('Location',parameters.cbarLocation)
end
axis xy
yLabelStr='Gal1 Fluorescence A.U.';
%xlabel(label{1},'FontName','Times-Roman','FontSize',labelFS)
%ylabel(yLabelStr,'FontName','Times-Roman','FontSize',labelFS)
title(label{2},'FontName','Times-Roman','FontSize',labelFS)
if(strcmp(parameters.drawLine,'true'))
    transPointX=parameters.transPointX;
    transOffsetX=-printedXsize*0.2;
    text(transPointX+transOffsetX,max(yplotRange)*0.9,num2str(transPointX),...
        'Color',[0.99 0.99 0.99],'BackgroundColor','none' ,'FontName','Times-Roman','FontSize',axisFS);
    line([transPointX transPointX],[min(yplotRange) max(yplotRange)],'Color',[0.99 0.99 0.99],'LineWidth',0.75)
end
aHand=get(plot_fig,'CurrentAxes');

set(aHand,'FontName','Times-Roman','FontSize',axisFS)
set(aHand,'Color','none');
set(plot_fig, 'Color', 'none');

if(strcmp(parameters.print,'true'))
    print(plot_fig, '-depsc2','-painters', label{3});
end
%figure
%colorbar('peer',aHand)
%colorbar

function [minVal deltaBin]=compute_logFscAxis(xrange,xbins)
%for values of xbins/maxShift >1 the minimum value will shift by an amount
%equal to fix(xbins/maxShift).  Note this is not a problem for a regular
%binning operation since in that case the bins are being reduced (ie
%maxShift or the range of values will be greater then xbins).  In our case
%this is usually not the case.  If a correction is not made then the axis
%will not be scaled properly.  The true minimum value is
%(maxShift/xbins)+minlogval or xrange(1)+(maxShift/xbins)-1.  The
%difference between bins becomes
%(xrange(2)-(xrange(1)+(maxShift/xbins)-1)/xbins
minLogVal=xrange(1)-1;
%maxLogVal determines the maximum log value that will be binned.  All
%values above this will be compressed into the last bin.
maxLogVal=xrange(2);
%shift the maximum log value
maxShift=maxLogVal-minLogVal;    
minVal=xrange(1)+(maxShift/xbins)-1;
%note the xbins is subtracted by one to make sure there are xbin spaces
%between the min and max values
deltaBin=(xrange(2)-minVal)/(xbins-1);

function [toPlot]=findPlottedIndices(PlotAxis,plotRangeForAxis)
toPlot=PlotAxis>plotRangeForAxis(1) & PlotAxis<plotRangeForAxis(2);

